<template>
  <div>
    <ul class="list">
      <li class="item"
          v-for="(city, key) in cities" :key="key"
          @click="handleClick"
          @touchstart.prevent="handleTouchStart"
          @touchmove="handleTouchMove"
      >
        {{key}}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  props: {
    cities: Object
  },
  data () {
    return {
      baseOffsetTop: 154,
      timer: null
    }
  },
  computed: {
    letters () {
      const letters = []
      for (let i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  },
  methods: {
    handleClick (e) {
      this.$emit('change', e.target.innerText)
    },
    handleTouchStart (e) {
      this.$emit('change', e.target.innerText)
    },
    handleTouchMove (e) {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        let moveDistance = e.touches[0].clientY - this.baseOffsetTop
        let num = Math.floor(moveDistance / 20)
        this.$emit('change', this.letters[num])
      }, 10)
    }
  }
}
</script>

<style lang='stylus' rel='stylesheet/stylus' scoped>
  @import '~styles/varibles.styl'
  .list
    display flex
    flex-direction column
    justify-content center
    position absolute
    top 1.6rem
    right 0
    bottom 0
    width .4rem
    .item
      line-height .4rem
      text-align center
      color $bgColor
</style>
